package com.castle.fortress.admin.member.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.annotation.CastleLog;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.member.dto.MemberAuthorityDto;
import com.castle.fortress.admin.member.entity.MemberAuthorityEntity;
import com.castle.fortress.admin.member.service.MemberAuthorityService;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员可见权限表 api 控制器
 *
 * @author castle
 * @since 2023-02-14
 */
@Api(tags="会员可见权限表api管理控制器")
@RestController
@RequestMapping("/api/member/memberAuthority")
public class ApiMemberAuthorityController {
    @Autowired
    private MemberAuthorityService memberAuthorityService;


    /**
     * 会员可见权限表的分页展示
     * @param memberAuthorityDto 会员可见权限表实体类
     * @param currentPage 当前页
     * @param size  每页记录数
     * @return
     */
    @CastleLog(operLocation="会员可见权限表-api",operType = OperationTypeEnum.QUERY)
    @ApiOperation("会员可见权限表分页展示")
    @GetMapping("/page")
    public RespBody<IPage<MemberAuthorityDto>> pageMemberAuthority(MemberAuthorityDto memberAuthorityDto, @RequestParam(required = false) Integer currentPage, @RequestParam(required = false)Integer size){
        Integer pageIndex= currentPage==null? GlobalConstants.DEFAULT_PAGE_INDEX:currentPage;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<MemberAuthorityDto> page = new Page(pageIndex, pageSize);

        IPage<MemberAuthorityDto> pages = memberAuthorityService.pageMemberAuthority(page, memberAuthorityDto);
        return RespBody.data(pages);
    }

    /**
     * 会员可见权限表保存
     * @param memberAuthorityDto 会员可见权限表实体类
     * @return
     */
    @CastleLog(operLocation="会员可见权限表-api",operType = OperationTypeEnum.INSERT)
    @ApiOperation("会员可见权限表保存")
    @PostMapping("/save")
    public RespBody<String> saveMemberAuthority(@RequestBody MemberAuthorityDto memberAuthorityDto){
        if(memberAuthorityDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            MemberAuthorityEntity memberAuthorityEntity = ConvertUtil.transformObj(memberAuthorityDto,MemberAuthorityEntity.class);
        if(memberAuthorityService.save(memberAuthorityEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员可见权限表编辑
     * @param memberAuthorityDto 会员可见权限表实体类
     * @return
     */
    @CastleLog(operLocation="会员可见权限表-api",operType = OperationTypeEnum.UPDATE)
    @ApiOperation("会员可见权限表编辑")
    @PostMapping("/edit")
    public RespBody<String> updateMemberAuthority(@RequestBody MemberAuthorityDto memberAuthorityDto){
        if(memberAuthorityDto == null || memberAuthorityDto.getId() == null || memberAuthorityDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            MemberAuthorityEntity memberAuthorityEntity = ConvertUtil.transformObj(memberAuthorityDto,MemberAuthorityEntity.class);
        if(memberAuthorityService.updateById(memberAuthorityEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员可见权限表删除
     * @param ids 会员可见权限表id集合
     * @return
     */
    @CastleLog(operLocation="会员可见权限表-api",operType = OperationTypeEnum.DELETE)
    @ApiOperation("会员可见权限表删除")
    @PostMapping("/delete")
    public RespBody<String> deleteMemberAuthority(@RequestBody List<Long> ids){
        if(ids == null || ids.isEmpty()){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(memberAuthorityService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员可见权限表详情
     * @param id 会员可见权限表id
     * @return
     */
    @CastleLog(operLocation="会员可见权限表-api",operType = OperationTypeEnum.QUERY)
    @ApiOperation("会员可见权限表详情")
    @GetMapping("/info")
    public RespBody<MemberAuthorityDto> infoMemberAuthority(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            MemberAuthorityEntity memberAuthorityEntity = memberAuthorityService.getById(id);
            MemberAuthorityDto memberAuthorityDto = ConvertUtil.transformObj(memberAuthorityEntity,MemberAuthorityDto.class);
        return RespBody.data(memberAuthorityDto);
    }


}
